/*
 * Copyright 2015-2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.wywuzh.commons.mybatis.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

import javax.sql.DataSource;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import io.github.wywuzh.commons.mybatis.constants.DataSourceConstants;

/**
 * 类DruidDataSourceConfig的实现描述：Alibaba Druid 数据源配置
 *
 * @author <a href="mailto:wywuzh@163.com">伍章红</a> 2020-12-29 17:13:55
 * @version v2.3.6
 * @since JDK 1.8
 */
@Configuration
@ConditionalOnClass(DruidDataSource.class)
public class DruidDataSourceConfig {

    @Primary
    @Bean(name = DataSourceConstants.BEAN_NAME_WRITE, initMethod = "init", destroyMethod = "close")
    @ConfigurationProperties(DataSourceConstants.PROPERTIES_PREFIX_WRITE)
    @ConditionalOnMissingBean(name = DataSourceConstants.BEAN_NAME_WRITE)
    public DataSource writeDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = DataSourceConstants.BEAN_NAME_READ, initMethod = "init", destroyMethod = "close")
    @ConfigurationProperties(DataSourceConstants.PROPERTIES_PREFIX_READ)
    @ConditionalOnMissingBean(name = DataSourceConstants.BEAN_NAME_READ)
    public DataSource readDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

}
